/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



var newcategoryparentid = 0;
var newcategoryparentname ="";
var newsubcategoryparentid = 0;
var newsubcategoryparentname ="";
var newcattype=0;
var newcatparentid =0;
var iseditcategory=0;
var newcategoryidforrename=0;
            
        
$( "#select-category" ).empty();
        
var categoryorder;
$(document).ready(function() {
    $( "#sortable1, #sortable2, #sortable3" ).sortable({
        //connectWith: ".connectedSortable"
        update: function( event, ui ) {
            categoryorder = $(this).sortable('toArray').toString();
            alert('order updated' + categoryorder);
            $.ajax({
                type: "get",
                url: "../editcategory",
                datatype: "json",
                data: "edittype=reorder&categoryorder="+categoryorder,
                success: function(data){
                },
                error: function(jqXHR, textStatus) { 
                    alert( "Request failed: " + textStatus ); 
                }
            });
        }
    }).disableSelection();
        
    $( "#sortable1, #sortable2, #sortable3" ).sortable("disable");
    
    $(document).bind("contextmenu", function(e) {
        return false;
    });
             
    $( "#addcategory" ).dialog({
        autoOpen: false,
        height: 200,
        width: 500,
        modal: true,
        title: "Add Category",
        resizable : false,
        buttons: {
            Save: function() {
                if ($("#categoryname").val() !="" ){
                    createnewcategory($("#categoryname").val(), newcatparentid);
                    if(newcattype==1){
                        alert("Module Saved");
                        getmodules();
                    }else if (newcattype ==2){
                        alert("Category Saved");
                        getCategories(newcategoryparentid, newcategoryparentname);
                    }else if ( newcattype==3){
                        alert("SubCategory Saved");
                        getSubCategories(newsubcategoryparentid, newsubcategoryparentname);
                    }else{
                        alert("Error");
                    }
                }else{
                    alert("Please add category name");
                }
            },
            OK: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
        }
    });
            
    $( "#renamecategory" ).dialog({
        autoOpen: false,
        height: 200,
        width: 500,
        modal: true,
        title: "Rename Category",
        resizable : false,
        buttons: {
            Save: function() {
                if ($("#newcategoryname").val() !="" ){
                    $.ajax({
                        type: "get",
                        url: "../editcategory",
                        datatype: "json",
                        data: "edittype=rename&categoryname="+$("#newcategoryname").val()+"&categoryid="+newcategoryidforrename,
                        success: function(data){
                            $("#" + newcategoryidforrename).css("color", "Blue");
                            $("#" + newcategoryidforrename).css("font-weight", "bold");
                        },
                        error: function(jqXHR, textStatus) { 
                            alert( "Request failed: " + textStatus ); 
                        }
                    });
                }else{
                    alert("Please add category name");
                }
            },
            OK: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
        }
    });       
            
            
    $.ajax({
        type: "get",
        url: "../GetModules",
        datatype: "json",
        data: "parentid=0",
        success: function(data){
            $("#sortable2").html("<li class=\"ui-state-highlight\">Select Module</li>");
            $("#sortable3").html("<li class=\"ui-state-highlight\">Select category</li>");
            for ( i= 0; i < data.modules.length; i++){
                if (iseditcategory==1){
                    $("#sortable1").append("<li id=\"" + data.modules[i].id + "\" onclick=\"getCategories('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\"  class=\"ui-state-default list1\"><img src=\"../images/Edit.png\"  title=\"Rename Category\" style=\"display: inline\"  width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;<img src=\"../images/SettingsDelete.png\"  style=\"display: inline\"  title=\"Delete Category\" onclick=\"deletecategory('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;" + data.modules[i].sectionname + "</li>");
                }else if(iseditcategory==0){
                    $("#sortable1").append("<li id=\"" + data.modules[i].id + "\" onclick=\"getCategories('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\"  class=\"ui-state-default list1\">" + data.modules[i].sectionname + "</li>");
                } 
            }
        },
        error: function(jqXHR, textStatus) { 
            alert( "Request failed: " + textStatus ); 
        }
    });
});
            
            
            
        
function getmodules(){
    $("#sortable1").html("");
    $.ajax({
        type: "get",
        url: "../GetModules",
        datatype: "json",
        data: "parentid=0",
        success: function(data){
            $("#sortable2").html("<li class=\"ui-state-highlight\">Select Module</li>");
            $("#sortable3").html("<li class=\"ui-state-highlight\">Select category</li>");
            for ( i= 0; i < data.modules.length; i++){
                // $("#sortable1").append("<li id=\"" + data.modules[i].id + "\" onclick=\"getCategories('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" class=\"ui-state-default list1\">" + data.modules[i].sectionname + "</li>");
                if (iseditcategory==1){
                    $("#sortable1").append("<li id=\"" + data.modules[i].id + "\" onclick=\"getCategories('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\"  class=\"ui-state-default list1\"><img src=\"../images/Edit.png\"  title=\"Rename Category\" style=\"display: inline\" onclick=\"renamecategory('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;<img src=\"../images/SettingsDelete.png\"  title=\"Delete Category\"  style=\"display: inline\" onclick=\"deletecategory('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;" + data.modules[i].sectionname + "</li>");
                }else if(iseditcategory==0){
                    $("#sortable1").append("<li id=\"" + data.modules[i].id + "\" onclick=\"getCategories('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\"  class=\"ui-state-default list1\">" + data.modules[i].sectionname + "</li>");
                }    
            }
            newcategoryparentid = 0;
            newcategoryparentname ="";
            newsubcategoryparentid = 0;
            newsubcategoryparentname ="";
                        
        },
        error: function(jqXHR, textStatus) { 
            alert( "Request failed: " + textStatus ); 
        }
    });
    
}
           
function getCategories(parentcategoryid, parentcategoryname){
    $(".list1").removeClass("ui-state-highlight");
    $("#" + parentcategoryid).addClass("ui-state-highlight");
    newcategoryparentid = parentcategoryid;
    newcategoryparentname=parentcategoryname;
    $.ajax({
        type: "get",
        url: "../GetModules",
        datatype: "json",
        data: "parentid="+parentcategoryid,
        success: function(data){
            $("#sortable2").html("");
            $("#sortable3").html("<li class=\"ui-state-highlight\">Select category</li>");
            if (data.modules.length >0){
                for ( i= 0; i < data.modules.length; i++){
                    if (iseditcategory==1){
                        $("#sortable2").append("<li id=\"" + data.modules[i].id + "\" onclick=\"getSubCategories('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\"  class=\"ui-state-default list2\"><img src=\"../images/Edit.png\"  title=\"Rename Category\" style=\"display: inline\" onclick=\"renamecategory('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;<img src=\"../images/SettingsDelete.png\" title=\"Delete Category\" style=\"display: inline\" onclick=\"deletecategory('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;" + data.modules[i].sectionname + "</li>");
                    }else{
                        $("#sortable2").append("<li id=\"" + data.modules[i].id + "\" onclick=\"getSubCategories('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\"  class=\"ui-state-default list2\">" + data.modules[i].sectionname + "</li>");
                    }
                                
                }
            }
            else{
                $("#sortable2").html("<li class=\"ui-state-default\">No Items found</li>");
            }
        },
        error: function(jqXHR, textStatus) { 
            alert( "Request failed: " + textStatus ); 
        }
    });
}
        
function getSubCategories (parentcategoryid, parentcategoryname) {
    $(".list2").removeClass("ui-state-highlight");
    $("#" + parentcategoryid).addClass("ui-state-highlight");
    newsubcategoryparentid = parentcategoryid;
    newsubcategoryparentname=parentcategoryname;
    $.ajax({
        type: "get",
        url: "../GetModules",
        datatype: "json",
        data: "parentid="+parentcategoryid,
        success: function(data){
            $("#sortable3").html("");
            if( data.modules.length >0 ){
                for ( i= 0; i < data.modules.length; i++){
                    if (iseditcategory==1){
                        $("#sortable3").append( "<li id=\"" + data.modules[i].id + "\" class=\"ui-state-default\"><img src=\"../images/Edit.png\"  style=\"display: inline\"  title=\"Rename Category\" onclick=\"renamecategory('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;<img src=\"../images/SettingsDelete.png\"  style=\"display: inline\"  title=\"Delete Category\" onclick=\"deletecategory('"+data.modules[i].id +"','"+ data.modules[i].sectionname+"')\" width=\"16\" height=\"16\"/>&nbsp; &nbsp;&nbsp;"+ data.modules[i].sectionname+"</li>");
                    }else if(iseditcategory==0){
                        $("#sortable3").append( "<li id=\"" + data.modules[i].id + "\" class=\"ui-state-default\">"+ data.modules[i].sectionname+"</li>");
                    } 
                            
                } 
            }else{
                $("#sortable3").html("<li class=\"ui-state-default\">No Items found</li>");
            }
        },
        error: function(jqXHR, textStatus) { 
            alert( "Request failed: " + textStatus ); 
        }
    });
}

            
function addmodulefn(categorytype){
           
    if ( categorytype == 1){
        $( "#select-category" ).html("");
        newcattype=1;
        newcatparentid=0;
    } else if ( categorytype==2){
        $( "#select-category" ).html("");
        newcattype=2;
        if (newcategoryparentname ==""){
            $( "#select-category" ).html("<span style=\" color: red\">No Module Selected</span>");
        }else{
            $( "#select-category" ).html(newcategoryparentname);
            newcatparentid=newcategoryparentid;
        }
    } else if ( categorytype ==3){
        $( "#select-category" ).html("");
        newcattype=3;
        if (newsubcategoryparentname ==""){
            $( "#select-category" ).html("<span style=\" color: red\">No Category Selected</span>");
        }else{
            $( "#select-category" ).html(newsubcategoryparentname);
            newcatparentid=newsubcategoryparentid;
        }
    }else{
        alert("Categorytype = " +categorytype);
    }
    $( "#categoryname" ).val('');
    $( "#addcategory" ).dialog( "open" );
            
            
}
        
 
function createnewcategory (categoryname, parentid) {
    $.ajax({
        type: "get",
        url: "../addcategory",
        datatype: "json",
        data: "categoryname="+categoryname+"&parentid="+parentid,
        success: function(data){
        },
        error: function(jqXHR, textStatus) { 
            alert( "Request failed: " + textStatus ); 
        }
    });
}
    
function editcategory(){
    if ( iseditcategory==0){
        $("#editimgid").attr("src","../images/SettingsDone.png");
        iseditcategory=1;
    }else{
        $("#editimgid").attr("src","../images/EditMenu.png");
        newcategoryparentid = 0;
        newcategoryparentname ="";
        newsubcategoryparentid = 0;
        newsubcategoryparentname ="";
        iseditcategory=0;
    }
                
    getmodules();
}

function deletecategory(categoryid, categoryname){
    alert("Are you sure you want to delete " + categoryname);
                
    $("#" + categoryid).css("color", "red");
    $("#" + categoryid).css("font-weight", "bold");
    $.ajax({
        type: "get",
        url: "../editcategory",
        datatype: "json",
        data: "edittype=delete&categoryname="+categoryname+"&categoryid="+categoryid,
        success: function(data){
        },
        error: function(jqXHR, textStatus) { 
            alert( "Request failed: " + textStatus ); 
        }
    });
}
function renamecategory(categoryid, categoryname){
    newcategoryidforrename = categoryid;
    //alert("Rename category ? " + categoryname);
    $( "#categorytorename" ).html("");
    $( "#categorytorename" ).html("" + categoryname);
    $( "#newcategoryname" ).val('');
    $( "#renamecategory" ).dialog( "open" );
}
            a
            
var isreordercategory=0;
            
function reordercategory(){
    alert("Reorder Categories");
    $( "#sortable1, #sortable2, #sortable3" ).sortable("enable");
    if ( isreordercategory==0){
        $("#reorderimgid").attr("src","../images/SettingsDone.png");
        isreordercategory=1;
    }else{
        $("#reorderimgid").attr("src","../images/ReorderMenu.png");
        isreordercategory=0;
    }
}
function savesettings(){
    //alert("Save Settings");
    if (iseditcategory==1){
        iseditcategory=0;
        getmodules();
    }
}